﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace UNSWThink
{
   /// <summary>
   /// Windows form used for adding/editing multiple choice answers
   /// </summary>
   public partial class AddEditAnswerForm : Form
   {
      private bool mode;
      private DBConnection dbConnection;
      private int answerID;
      private DataTable answerTable;
      private int questionID;
      private int answerRowIndex;

      /// <summary>
      /// Default constructor
      /// </summary>
      /// <param name="mode">
      /// Mode used to determine whether the answer is to be added or edited
      /// </param>
      /// <param name="dbConnection">
      /// DBConnection object used to access a database
      /// </param>
      /// <param name="questionID">
      /// ID of the question that it's answers are being added/edited
      /// </param>
      /// <param name="answerID">
      /// ID of the answer that is being edited
      /// </param>
      /// <param name="answerTable">
      /// Data table of answers
      /// </param>
      public AddEditAnswerForm(bool mode, DBConnection dbConnection, int questionID, int answerID, DataTable answerTable)
      {
         InitializeComponent();

         this.mode = mode;
         this.dbConnection = dbConnection;
         this.questionID = questionID;
         this.answerID = answerID;
         this.answerTable = answerTable;

         this.answerRowIndex = 0;

         if (mode == Defs.ADD_MODE)
         {
            this.answerID = 0;
            this.Text = "Add an answer";
            buttonAddAnswer.Enabled = false;
         }
         else
         {
            this.Text = "Edit an answer";
            buttonAddAnswer.Enabled = true;
            buttonAddAnswer.Text = "OK";
         }
      }

      #region WINDOWS FORM EVENTS

      private void AddEditAnswerForm_Load(object sender, EventArgs e)
      {
         if (mode == Defs.EDIT_MODE)
         {
            answerRowIndex = HelperMethods.GetIndexOfTableID(answerTable, answerID);
            textBoxAnswerDescription.Text = answerTable.Rows[answerRowIndex]["description"].ToString();
            checkBoxAnswerCorrect.Checked = (bool)answerTable.Rows[answerRowIndex]["iscorrect"];
         }
      }

      #endregion

      #region CONTROL EVENTS

      private void buttonCancel_Click(object sender, EventArgs e)
      {
         this.Close();
      }

      private void textBoxAnswerDescription_TextChanged(object sender, EventArgs e)
      {
         if (textBoxAnswerDescription.Text != "")
         {
            buttonAddAnswer.Enabled = true;
         }
         else
         {
            buttonAddAnswer.Enabled = false;
         }
      }

      private void buttonAddAnswer_Click(object sender, EventArgs e)
      {
         // check that the form has been completed properly
         if (CheckCompleted())
         {
            // init all necessary variables for insert/update
            if (mode == Defs.EDIT_MODE)
            {
               // questionID already exists when in edit mode
               questionID = (int)answerTable.Rows[answerRowIndex]["question"];
            }
            string description = textBoxAnswerDescription.Text;
            bool isCorrect = checkBoxAnswerCorrect.Checked;
            string image = "NONE";

            string[] columnNames = new string[4] { 
                  "question",
                  "description",
                  "iscorrect",
                  "image"};
            object[] values = new object[4] {
                  questionID,
                  description,
                  isCorrect,
                  image};

            // insert/update program's memory with latest answer data
            if (mode == Defs.ADD_MODE)
            {
               object[] newAnswer = new object[5] { 0, 0, description, isCorrect, image };
               answerTable.Rows.Add(newAnswer);
            }
            else
            {
               for (int i = 0; i < columnNames.Length; i++)
               {
                  answerTable.Columns[columnNames[i]].ReadOnly = false;
                  answerTable.Rows[answerRowIndex][columnNames[i]] = values[i];
                  answerTable.Columns[columnNames[i]].ReadOnly = true;
               }
            }

            this.Close();
         }
         else
         {
            string msg = "";
            string msgTitle = "";
            if (mode == Defs.ADD_MODE)
            {
               msg = "Cannot add the new answer.\nMake sure that all necessary details are completed";
               msgTitle = "Cannot add new answer";
            }
            else
            {
               msg = "Cannot update the current answer.\nMake sure that all necessary details have been completed";
               msgTitle = "Cannot update current answer";
            }

            MessageBox.Show(msg, msgTitle, MessageBoxButtons.OK, MessageBoxIcon.Hand);
         }
      }

      #endregion

      #region HELPER METHODS

      // checks if the form has been completed
      private bool CheckCompleted()
      {
         if (textBoxAnswerDescription.Text != "")
         {
            return true;
         }

         return false;
      }

      #endregion
   }
}
